/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "Stdafx.h"
#include "boost\regex.hpp"
#include "BaiduPageParser.h"
#include "HelperFunctions.h"
#include "BaiduDecodeUrlHelperFunction.h"
using namespace std;
using namespace boost;

void CBaiduSongParser::Parse(const tstring & Content)
{
     // Clear the results set
     m_SongList.clear();

     // Regex repression
     tregex reg_song(_T("<td class=tdn>(\\d+)</td>.*?<td class=d><a href=\"(.+?)\".+?")
                     _T("<td>(.+?)</td>.*?")
                     _T("<td class=al>(.+?)</td>.*?")
                     _T("<td>.+?</td>.*?")
                     _T("<td>.+?</td>.*?")
                     _T("<td>.+?</td>.*?")
                     _T("<td>(.+?)</td>.*?")
                     _T("<td>(.+?)</td>.*?")
                     _T("<td class=spd><img src=\"(.+?)\"></td>"));
     tregex reg_name(_T("baidusg,(.+?)&"));
     tregex reg_singer(_T("<a.+?>(.+?)</a>"));
     tregex reg_album(_T("<a .+?>(.+?)</a>"));
     tregex reg_removeFont(_T("<font.*>(.+?)</font>"));

     tsmatch name;
     tsmatch song;
     tsmatch singer;
     tsmatch album;
       
     tstring::const_iterator bgn = Content.begin();
     tstring::const_iterator end = Content.end();

     while( regex_search(bgn, end, song, reg_song) )
     {
            bgn = song[0].second;
    
            // Song name
            tstring url = song[2].str();
            tstring SongName = _T("");
            if( regex_search(url, name, reg_name) )
                SongName = name[1].str();  
            // Trim spaces
            TrimStr(SongName);

            // Singer
            tstring sig = song[3].str();        
            if( regex_search(sig, singer, reg_singer) ) {
                sig = singer[1].str();
                if( regex_search(sig, singer, reg_removeFont) )
                    sig = singer[1].str();
            } else
                sig = _T("");

            // Album
            tstring alb = song[4].str();
            if( regex_search(alb, album, reg_album) ) {
                alb = album[1].str();   
                if( regex_search(alb, album, reg_removeFont) )
                    alb = album[1].str();   
            } else
                alb = _T("");
                   
            SongProperty Sp = SongProperty(song[1].str(),//index
                                           SongName,     //song name
                                           song[2].str(),//song url
                                           sig,          //singer name
                                           alb,          //album 
                                           song[5].str(),//size in string fromat
                                           song[6].str());//format
            m_SongList.push_back(Sp);
     }
}
     
void CBaiduSongParser::GetSongList(SongList & List)const
{
     // Clear list
     List.clear();
     // Get size
     unsigned int Size = m_SongList.size();
     // Reverse space
     if( Size == 0 )
         return;
     List.reserve(Size);
     // Copy
     for( unsigned int i = 0; i < Size; ++i )
     {
          SongProperty Sp = m_SongList[i];
          List.push_back(Sp);
     }
}


void CBaiduSongNumParser::Parse(const tstring & Content)
{
     // Regex repression
     tregex Reg_SongNum(_T("<td align=\"right\" nowrap>.+?([\\d,,]+).+?</td>"));
     tsmatch Num;
     if( regex_search(Content.begin(), Content.end(), Num, Reg_SongNum) )
         m_TotalSongNum = FormatNum(Num[1].str());
     else
         m_TotalSongNum = 0;
}

void CBaiduSongUrlParser::Parse(const tstring & Content)
{
     tregex Reg_Url(_T("var sertim = (\\d+).+")
                    _T("var encurl.+?\"(.+?)\""));
     tsmatch Url;
     if( regex_search(Content, Url, Reg_Url) )
     {
         unsigned int BaiduMagicNumber =  _ttoi(Url[1].str().c_str());
         tstring EncodeUrl = Url[2].str();
         m_DownloadUrl = BaiduMp3DownloadUrlDecode(EncodeUrl, BaiduMagicNumber);
     }
}
